package com.xiyun.admin.controller;

import com.xiyun.common.response.result.R;
import com.xiyun.admin.service.DepartmentService;
import com.xiyun.admin.utils.excel.data.ExcelData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@Api(description = "部门管理")
@RestController
@RequestMapping("/xiyun/service/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("上传Excel文件")
    @PostMapping("uploadexcel")
    public R uploadExcel(MultipartFile file){
        if (!"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(file.getContentType())){
            System.out.println("上传文件类型有误,无法解析");
            return R.error();
        }
        try {
            departmentService.readExcel(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @ApiOperation("下载Excel文件")
    @GetMapping("downloadexcel")
    public R downloadExcel(){
        List<ExcelData> list = departmentService.downloadExcel();
        return R.ok().data("excel", list);
    }
}
